package io.github.wslxm.springbootplus2.manage.gc.generate.util;

import io.github.wslxm.springbootplus2.core.enums.BaseDic;
import io.github.wslxm.springbootplus2.manage.gc.config.GcConfig;
import io.github.wslxm.springbootplus2.manage.gc.constant.BracketConstant;
import io.github.wslxm.springbootplus2.manage.gc.constant.FieldTypeConstant;
import io.github.wslxm.springbootplus2.manage.gc.template.vue2.Vue2AddUpdSlotTemplate;
import io.github.wslxm.springbootplus2.manage.gc.template.vue2.Vue2AddUpdTemplate;
import io.github.wslxm.springbootplus2.manage.gc.template.vue2.Vue2MainSlotTemplate;
import io.github.wslxm.springbootplus2.manage.gc.template.vue2.Vue2MainTemplate;
import io.github.wslxm.springbootplus2.manage.gc.utils.GcDataUtil;


import java.util.List;

/**
 * 生成vue2 代码工具
 *
 * @author wangsong
 * @date 2023/08/28
 */
public class GenerateVue2Util {


    private final static String NO = "NO";

    /**
     * 获取vue 添加或编辑页的 表单数据
     *
     * @param gcConfig     代码生成配置信息
     * @param name         字段名
     * @param type         字段类型
     * @param typeDetail   类型+长度
     * @param newDesc      字段中文描述
     * @param vueFieldType vue表单字段类型
     * @param isNull       是否必填 NO 代表必填, YES 非必填
     * @return
     */
    public static String jxVue2AddOrUpdColumns(GcConfig gcConfig, String name, String type, String typeDetail, String newDesc, Integer vueFieldType, List<String> dictCode, String isNull) {
        boolean required = isNull.equals(NO);
        // 生成表单时获取数据库的字段的长度来控制输入
        String maxlength = "0";
        // 小数位
        int precision = 0;
        if (type.equals(FieldTypeConstant.INT) || type.equals(FieldTypeConstant.BIGINT) || type.equals(FieldTypeConstant.TINYINT)) {
            // int(11), 判断是否有长度,存在长度获取指定长度的最大值, 转为long添加到注解中
            if (typeDetail.contains(BracketConstant.LEFT_BRACKET)) {
                int len = Integer.parseInt(typeDetail.substring(typeDetail.indexOf(BracketConstant.LEFT_BRACKET) + 1, typeDetail.indexOf(BracketConstant.RIGHT_BRACKET)));
                maxlength = GenerateUtil.numberGenerator(9, len, 0);
            } else {
                // 默认大小
                maxlength = GenerateUtil.getDefaultMaxlength(type);
            }
        } else if (type.equals(FieldTypeConstant.DOUBLE) || type.equals(FieldTypeConstant.FLOAT) || type.equals(FieldTypeConstant.DECIMAL)) {
            //  小数
            //  判断是否有长度,存在长度获取指定长度的最大值, 转为long添加到注解中 decimal(10,2)，取10, 2不处理
            if (typeDetail.contains(BracketConstant.LEFT_BRACKET)) {
                String typeDetailStr = typeDetail.substring(typeDetail.indexOf(BracketConstant.LEFT_BRACKET) + 1, typeDetail.indexOf(BracketConstant.RIGHT_BRACKET));
                String[] typeDetailStrArray = typeDetailStr.split(",");
                if (typeDetailStrArray.length > 1) {
                    maxlength = GenerateUtil.numberGenerator(9, Integer.parseInt(typeDetailStrArray[0]), Integer.parseInt(typeDetailStrArray[1]));
                } else {
                    maxlength = GenerateUtil.numberGenerator(9, Integer.parseInt(typeDetailStrArray[0]), 0);
                }
            } else {
                // 默认大小
                maxlength = GenerateUtil.getDefaultMaxlength(type);
            }
        } else if (type.equals(FieldTypeConstant.VARCHAR) || type.equals(FieldTypeConstant.CHAR) || type.equals(FieldTypeConstant.TEXT) || type.equals(FieldTypeConstant.LONG_TEXT)) {
            // 字符串
            if (typeDetail.contains(BracketConstant.LEFT_BRACKET)) {
                maxlength = typeDetail.substring(typeDetail.indexOf(BracketConstant.LEFT_BRACKET) + 1, typeDetail.indexOf(BracketConstant.RIGHT_BRACKET));
            }
        } else if (type.equals(FieldTypeConstant.DATETIME) || type.equals(FieldTypeConstant.TIME) || type.equals(FieldTypeConstant.TIMESTAMP)) {
        }
        // 处理字段
        String columnStr = "";
        name = GcDataUtil.getFieldName(gcConfig, name);
        if (BaseDic.VueFieldType.V1.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.INPUT.replace("{maxlength}", maxlength + "");
        } else if (BaseDic.VueFieldType.V2.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.NUMBER.replace("{precision}", precision + "").replace("{maxRows}", maxlength);
        } else if (BaseDic.VueFieldType.V3.getValue().equals(vueFieldType)) {
        } else if (BaseDic.VueFieldType.V4.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.RADIO.replace("{dictCode}", GenerateUtil.getDictCode(dictCode));
        } else if (BaseDic.VueFieldType.V5.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.CHECKBOX.replace("{dictCode}", GenerateUtil.getDictCode(dictCode));
        } else if (BaseDic.VueFieldType.V6.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.SELECT.replace("{dictCode}", GenerateUtil.getDictCode(dictCode));
        } else if (BaseDic.VueFieldType.V7.getValue().equals(vueFieldType)) {
        } else if (BaseDic.VueFieldType.V8.getValue().equals(vueFieldType)) {
        } else if (BaseDic.VueFieldType.V9.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.SWITCH.replace("{dictCode}", GenerateUtil.getDictCode(dictCode));
        } else if (BaseDic.VueFieldType.V10.getValue().equals(vueFieldType)) {
        } else if (BaseDic.VueFieldType.V11.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.DATETIME.replaceAll("\\{label}", newDesc).replace("{prop}", name);
        } else if (BaseDic.VueFieldType.V12.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.TIME.replaceAll("\\{label}", newDesc).replace("{prop}", name);
        } else if (BaseDic.VueFieldType.V13.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.UPLOAD.replace("{listType}", "picture-img").replace("{limit}", "1").replace("{fileType}", "img").replace("{accept}", "'image/png, image/jpeg, image/jpg, image/gif'").replace("{tip}", "只能上传 jpg/png/gif 格式的图片");
        } else if (BaseDic.VueFieldType.V14.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.UPLOAD.replace("{listType}", "picture-card").replace("{limit}", "10").replace("{fileType}", "img").replace("{accept}", "'image/png, image/jpeg, image/jpg, image/gif'").replace("{tip}", "只能上传10张 jpg/png/gif 格式的图片");
        } else if (BaseDic.VueFieldType.V15.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.UPLOAD.replace("{listType}", "picture-img").replace("{limit}", "1").replace("{fileType}", "video").replace("{accept}", "'video/mp4'").replace("{tip}", "只能上传mp4格式的视频");
        } else if (BaseDic.VueFieldType.V16.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.UPLOAD.replace("{listType}", "").replace("{limit}", "10").replace("{fileType}", "all").replace("{accept}", "null").replace("{tip}", "");
        } else if (BaseDic.VueFieldType.V17.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.TEXTAREA.replace("{maxlength}", maxlength + "");
        } else if (BaseDic.VueFieldType.V18.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.INPUT.replace("{maxlength}", maxlength + "");
        } else if (BaseDic.VueFieldType.V19.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.INPUT.replace("{maxlength}", maxlength + "");
        } else if (BaseDic.VueFieldType.V20.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.CASCADER;
        } else if (BaseDic.VueFieldType.V21.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.ARRAY;
        } else if (BaseDic.VueFieldType.V22.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.ICON;
        } else if (BaseDic.VueFieldType.V23.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.COLOR;
        } else if (BaseDic.VueFieldType.V24.getValue().equals(vueFieldType)) {
            columnStr = Vue2AddUpdTemplate.MAP;
        } else if (BaseDic.VueFieldType.V25.getValue().equals(vueFieldType)) {
            if (gcConfig.getIsTree()) {
                // 生成树结构时所有 tree 一律读取 this.treeData 树结构数据为选择数据
                columnStr = Vue2AddUpdTemplate.TREE.replace("{dicData}", "this.treeData");
            } else {
                String dictCodeStr = GenerateUtil.getDictCode(dictCode);
                columnStr = Vue2AddUpdTemplate.TREE.replace("{dicData}", "this.dict.get(" + dictCodeStr + ")");
            }
        } else {
            // 没有默认 input
            columnStr = Vue2AddUpdTemplate.INPUT.replace("{maxlength}", maxlength + "");
        }
        // 基础必填字段
        columnStr = columnStr.replaceAll("\\{label}", newDesc)
                .replaceAll("\\{prop}", name)
                .replace("{required}", required + "");
        return columnStr;
    }


    /**
     * vue 字段需要定义为插槽的处理 (添加和编辑页)
     *
     * @param vueFieldType vue表单字段类型
     * @param name         字段名
     * @author wangsong
     * @mail 1720696548@qq.com
     * @date 2022/5/14 0014 22:41
     * @version 1.0.0
     */
    public static String jxVue2AddOrUpdColumnsSlot(Integer vueFieldType, String name) {
        String vueAddUpdSlot = "";
        if (BaseDic.VueFieldType.V18.getValue().equals(vueFieldType)) {
            // 追加定义富文本插槽
            vueAddUpdSlot = Vue2AddUpdSlotTemplate.TINYMCE_EDITOR.replaceAll("\\{prop}", name);
        }
        if (BaseDic.VueFieldType.V19.getValue().equals(vueFieldType)) {
            // 追加定义md编辑器插槽
            vueAddUpdSlot = Vue2AddUpdSlotTemplate.MD_EDITOR.replaceAll("\\{prop}", name);
        }
        return vueAddUpdSlot;
    }


    /**
     * vue 字段需要定义为插槽的处理 (列表页，搜索插槽 或 特殊字段展示的插槽)
     *
     * @param vueFieldType vue表单字段类型
     * @param name         字段名
     * @param isSearch     是否搜索
     * @author wangsong
     * @mail 1720696548@qq.com
     * @date 2022/5/14 0014 22:41
     * @version 1.0.0
     */
    public static String jxVue2MainColumnsSlot(Integer vueFieldType, String name, boolean isSearch) {
        String vueAddUpdSlot = "";
        if (BaseDic.VueFieldType.V11.getValue().equals(vueFieldType)) {
            // 时间范围搜索插槽
            vueAddUpdSlot = Vue2MainSlotTemplate.DATE_PICKER.replace("{prop}", name);
        }
        return vueAddUpdSlot;
    }


    /**
     * vue2 生成字段处理
     *
     * @param vueFieldTypeInt vue int字段类型
     * @param vueInfoColumns vue信息列
     * @param newDesc 新desc
     * @param name 名字
     * @param isSearch 是搜索
     * @param dictCode dict类型代码
     */
    public static void vue2InfoColumns(GcConfig gcConfig, Integer vueFieldTypeInt, StringBuffer vueInfoColumns, String newDesc, String name, boolean isSearch, List<String> dictCode) {
        if (vueFieldTypeInt.equals(BaseDic.VueFieldType.V4.getValue())
                || vueFieldTypeInt.equals(BaseDic.VueFieldType.V6.getValue())
                || vueFieldTypeInt.equals(BaseDic.VueFieldType.V7.getValue())
                || vueFieldTypeInt.equals(BaseDic.VueFieldType.V9.getValue())) {
            // 字典 - 单选
            vueInfoColumns.append(Vue2MainTemplate.TEXT_DICT
                    .replaceAll("\\{label}", newDesc)
                    .replace("{prop}", name)
                    .replace("{search}", isSearch + "")
                    .replace("{dictCode}", GenerateUtil.getDictCode(dictCode))
            );
        } else if (vueFieldTypeInt.equals(BaseDic.VueFieldType.V5.getValue())
                || vueFieldTypeInt.equals(BaseDic.VueFieldType.V8.getValue())) {
            // 字典 - 多选
            vueInfoColumns.append(Vue2MainTemplate.TEXT_DICT_CHECKBOX
                    .replaceAll("\\{label}", newDesc).replace("{prop}", name)
                    .replace("{search}", isSearch + "").replace("{dictCode}", GenerateUtil.getDictCode(dictCode)));
        } else if (vueFieldTypeInt.equals(BaseDic.VueFieldType.V11.getValue())) {
            // 时间
            vueInfoColumns.append(Vue2MainTemplate.TEXT.replaceAll("\\{label}", newDesc).replace("{prop}", name)
                    .replace("{searchSpan}", "10").replace("{search}", isSearch + ""));
        } else if (vueFieldTypeInt.equals(BaseDic.VueFieldType.V12.getValue())) {
            // 时间-小时选择
            vueInfoColumns.append(Vue2MainTemplate.TIME.replaceAll("\\{label}", newDesc)
                    .replace("{prop}", name).replace("{search}", isSearch + ""));
        } else if (vueFieldTypeInt.equals(BaseDic.VueFieldType.V13.getValue()) || vueFieldTypeInt.equals(BaseDic.VueFieldType.V14.getValue())) {
            // 图片
            vueInfoColumns.append(Vue2MainTemplate.IMG.replaceAll("\\{label}", newDesc).replace("{prop}", name));
        } else if (vueFieldTypeInt.equals(BaseDic.VueFieldType.V20.getValue())) {
            // 级联选择器
            vueInfoColumns.append(Vue2MainTemplate.CASCADER.replace("{label}", newDesc).replace("{prop}", name)
                    .replace("{search}", isSearch + "")
            );
        } else if (vueFieldTypeInt.equals(BaseDic.VueFieldType.V22.getValue())) {
            // 图标选择器
            vueInfoColumns.append(Vue2MainTemplate.ICON.replace("{label}", newDesc).replaceAll("\\{prop}", name));
        } else if (vueFieldTypeInt.equals(BaseDic.VueFieldType.V23.getValue())) {
            // 颜色选择器
            vueInfoColumns.append(Vue2MainTemplate.COLOR.replace("{label}", newDesc).replace("{prop}", name));
        } else if (vueFieldTypeInt.equals(BaseDic.VueFieldType.V24.getValue())) {
            // 地图选择器
            vueInfoColumns.append(Vue2MainTemplate.MAP.replace("{label}", newDesc).replaceAll("\\{prop}", name));
        } else if (vueFieldTypeInt.equals(BaseDic.VueFieldType.V25.getValue())) {
            // Tree 树结构
            if (gcConfig.getIsTree()) {
                // 生成树结构时所有 tree 一律读取 this.treeData 树结构数据为选择数据
                vueInfoColumns.append(Vue2MainTemplate.TREE
                        .replace("{dicData}", "this.treeData")
                        .replace("{label}", newDesc)
                        .replaceAll("\\{prop}", name)
                        .replace("{search}", isSearch + ""));
            } else {
                String dictCodeStr = GenerateUtil.getDictCode(dictCode);
                vueInfoColumns.append(Vue2MainTemplate.TREE
                        .replace("{dicData}", "this.dict.get(" + dictCodeStr + ")")
                        .replace("{label}", newDesc)
                        .replaceAll("\\{prop}", name)
                        .replace("{search}", isSearch + ""));
            }
        } else {
            // 默认普通文本
            vueInfoColumns.append(Vue2MainTemplate.TEXT.replaceAll("\\{label}", newDesc).replace("{prop}", name)
                    .replace("{searchSpan}", "5").replace("{search}", isSearch + ""));
        }
    }
}
